package com.sea.base.ext.global

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract

/**
 * 安全的遍历删除
 */
inline fun <K, V> MutableMap<K, V>?.removeIf(filter: (k: K, v: V) -> Boolean) {
    if (isNullOrEmpty()) {
        return
    }
    val iterator = entries.iterator()
    while (iterator.hasNext()) {
        val next = iterator.next()
        if (filter(next.key, next.value)) {
            iterator.remove()
        }
    }
}

@Suppress("NOTHING_TO_INLINE")
@OptIn(ExperimentalContracts::class)
inline fun Map<*, *>?.isNotNullNotEmpty(): Boolean {
    contract {
        returns(true) implies (this@isNotNullNotEmpty != null)
    }
    return !this.isNullOrEmpty()
}